package com.ruitu.mall_98_2.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.MD5Util;
import com.beanu.arad.utils.RegexUtils;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class UpdatePasswordActivity extends MyBaseActivity {
    private EditText et_forget_tel;
    private EditText et_forget_password;
    private EditText et_forget_password_agin;
    private TextView tv_confirm_update;

    private String tel, password, password0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (tv_confirm_update == v) {
            tel = et_forget_tel.getText().toString();
            password = et_forget_password.getText().toString();
            password0 = et_forget_password_agin.getText().toString();
            if (!TextUtils.isEmpty(tel)) {
                if (TextUtils.isEmpty(password)) {
                    ToastUtils.showShort("原密码不能为空");
                    return;
                }
                if (RegexUtils.isPassword(password0)) {
                    showProgress();
                    setUpdate();
                } else {
                    ToastUtils.showShort("密码格式不正确");
                }
            }
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_update_password;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        et_forget_tel = findViewById(R.id.et_forget_tel);
        et_forget_password = findViewById(R.id.et_forget_password);
        et_forget_password_agin = findViewById(R.id.et_forget_password_agin);
        tv_confirm_update = findViewById(R.id.tv_confirm_update);
        tv_confirm_update.setOnClickListener(this);
    }

    private void setUpdate() {
        APIRetrofit.getDefault().updatePwd(AppHolder.instance().getUser().getId(), MD5Util.md5String(password), MD5Util.md5String(password0))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                        ToastUtils.showShort("修改密码失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        hideProgress();
                        if (baseResult.isSuccess()) {
                            Arad.preferences.putString("psw", password0).flush();
                            ToastUtils.showShort(baseResult.getMsg());
                            finish();
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }
}
